package com.kurumi.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kurumi.config.MyConfig;
import com.kurumi.mapper.BaseInfoMapper;
import com.kurumi.mapper.MedicalRecordMapper;
import com.kurumi.mapper.MedicalRecordTraceMapper;
import com.kurumi.pojo.MedicalRecord;
import com.kurumi.pojo.MedicalRecordPageTrace;
import com.kurumi.pojo.MedicalRecordTrace;
import com.kurumi.query.MedicalRecordQuery;
import com.kurumi.service.InterfaceMedicalRecord1Service;
import com.kurumi.util.DateUtil;
import com.kurumi.util.FileUtil;
import com.kurumi.util.JsonUtil;
import com.kurumi.util.StringUtil;
@Service
public class InterfaceMedicalRecord1ServiceImpl implements InterfaceMedicalRecord1Service {

	@Autowired
	private MyConfig myConfig;
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Autowired
	private MedicalRecordMapper medicalRecordMapper;
	
	@Autowired
	private MedicalRecordTraceMapper medicalRecordTraceMapper;
	
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int extractInterfaceMedicalRecordOfPage(MedicalRecord medicalRecord, Map<String, Object> jsonMap) throws Exception {
		// TODO Auto-generated method stub
		int count = 0;
		count = medicalRecordMapper.checkMeditalRecordUniq(medicalRecord.getOnlyId(), medicalRecord.getMrId(), medicalRecord.getVersion());
		if(count > 0){
			return 2;
		}
		Subject subject=SecurityUtils.getSubject();
		Session session = subject.getSession();
		Map<String, Object> currentUser = (Map<String, Object>)session.getAttribute("currentUser");
		String userCode =(String)currentUser.get("user_code");
		String userName =(String)currentUser.get("user_name");
		jsonMap.put("userCode", userCode);
		jsonMap.put("userName", userName);
		
		medicalRecord.setVersion(1);
		medicalRecord.setTreatmentSignCode("PT");
		medicalRecord.setTreatmentSignName("普通");
		count = medicalRecordMapper.insert(medicalRecord);
		if(count < 0){
			return -1;
		}
		List<Map<String,Object>> treatmentTraceTypes = baseInfoMapper.getTraceTypeByCode(MedicalRecordTrace.VISIT_INIT);
		if(treatmentTraceTypes.size() == 0){
			return -2;
		}
		
		MedicalRecordTrace medicalRecordTrace = new MedicalRecordTrace();
		
		medicalRecordTrace.setVisitGuid(medicalRecord.getVisitGuid());
		medicalRecordTrace.setCreateUserId(userCode);
		medicalRecordTrace.setCreateUserName(userName);
		medicalRecordTrace.setTraceTypeCode((String)treatmentTraceTypes.get(0).get("code"));
		medicalRecordTrace.setTraceTypeName((String)treatmentTraceTypes.get(0).get("name"));
		count = medicalRecordMapper.insertMedicalRecordTrace(medicalRecordTrace);
		treatmentTraceTypes = baseInfoMapper.getTraceTypeByCode(MedicalRecordTrace.VISIT_PIGEONHOLE);
		if(treatmentTraceTypes.size() == 0){
			return -2;
		}
		MedicalRecordTrace pigeonholeTrace = new MedicalRecordTrace();
		
		pigeonholeTrace.setVisitGuid(medicalRecord.getVisitGuid());
		pigeonholeTrace.setCreateUserId(userCode);
		pigeonholeTrace.setCreateUserName(userName);
		pigeonholeTrace.setTraceTypeCode((String)treatmentTraceTypes.get(0).get("code"));
		pigeonholeTrace.setTraceTypeName((String)treatmentTraceTypes.get(0).get("name"));
		
		pigeonholeTrace.setTraceDateTime(new Date());
		count = medicalRecordMapper.insertMedicalRecordTrace(pigeonholeTrace);
		
		MedicalRecordPageTrace signPageTrace = new MedicalRecordPageTrace();
		
		signPageTrace.setVisitGuid(medicalRecord.getVisitGuid());
		signPageTrace.setCreateUserId(userCode);
		signPageTrace.setCreateUserName(userName);
		signPageTrace.setTraceTypeCode(MedicalRecordPageTrace.VISIT_SIGN);
		signPageTrace.setTraceTypeName(MedicalRecordPageTrace.VISIT_SIGN_NAME);
		
		signPageTrace.setTraceDateTime(new Date());
		count = medicalRecordTraceMapper.insertMedicalRecordPageTrace(signPageTrace);
		
		String jsonMapJson = JsonUtil.objectToJson(jsonMap);
		String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(medicalRecord.getVisitGuid())+medicalRecord.getVisitGuid()+"\\";
		String versionFilePath = filePath+"version\\";
		String fileName = medicalRecord.getVisitGuid() + ".json";
		String versionFileName = medicalRecord.getVisitGuid()+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
		
		FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
		FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
		medicalRecordMapper.insertMedicalRecordJson(jsonMapJson);
		return count;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public int extractInterfaceMedicalRecordOfCost(String visitGuid,Map<String, Object> costMap) throws Exception {
		// TODO Auto-generated method stub
		String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(visitGuid)+visitGuid+"\\";
		String versionFilePath = filePath+"version\\";
		String fileName = visitGuid + ".json";
		String versionFileName = visitGuid+"-" + DateUtil.dateFormat("yyyyMMddHHmmssssss", new Date()) + ".json";
		
		/*String jsonData = FileUtil.readFile(filePath, fileName);*/
		List<String> jsonDatas = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		if(!jsonDatas.isEmpty()){
			jsonMap = JsonUtil.jsonToPojo(jsonDatas.get(0), Map.class);
				if(jsonMap == null){
				jsonMap = new HashMap<String, Object>();
			}
		}
		jsonMap.put("visitGuid", visitGuid);
		jsonMap.put("costInfo", costMap);
		String jsonMapJson = JsonUtil.objectToJson(jsonMap);
		
		FileUtil.createOrEditFile(jsonMapJson, filePath, fileName);
		FileUtil.createOrEditFile(jsonMapJson, versionFilePath, versionFileName);
		List<String> medicalRecordJsons = medicalRecordMapper.getMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
		
		if(medicalRecordJsons.isEmpty()){
			medicalRecordMapper.insertMedicalRecordJson(jsonMapJson);
		}else{
			medicalRecordMapper.deleteMedicalRecordJsonByVisitGuid(StringUtil.handleJsonParam(visitGuid));
			medicalRecordMapper.insertMedicalRecordJson(jsonMapJson);
		}
		return 1;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void restoreData() {
		MedicalRecordQuery medicalRecordQuery = new MedicalRecordQuery();
		List<String> list =	medicalRecordMapper.getMedicalRecoVisitGuid(medicalRecordQuery);
		int i = 0;
		for (String guid : list) {
			String filePath = myConfig.getJsonRecourcePath() + StringUtil.getLocalPath(guid)+guid+"\\";
			String fileName = guid + ".json";
			String json = FileUtil.readFile(filePath, fileName);
			if(json==null){
				continue;
			}
			medicalRecordMapper.insertMedicalRecordJson(json);
			System.out.println(i++);
		}
		
	}

}
